from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_protect
from django.template import Context, loader, RequestContext
from forum.models import Forum, Thread, ForumPost
from forum.forms import ThreadForm, ForumPostForm
import datetime

#Forum index view.  Shows a list of all the forums
def forum_index(request):
    forum_list = Forum.objects.all().order_by('order')
    template = loader.get_template('forum/forum_index.html')
    data = [('forum_list', forum_list),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))
    
    
#Thread list view.  Shows a list of all of the threads in a forum
#Also has options for forum moderators
def thread_list(request, forum_id):
    p = get_object_or_404(Forum, id=forum_id)
    template = loader.get_template('forum/thread_list.html')
    if request.method == 'POST':
        if not request.user.id: return HttpResponse("Error: Must be logged to modify threads.")
        if 'delete_thread' in request.POST:
            q = Thread.objects.get(id=request.POST['thread'])
            q.delete()
            return HttpResponseRedirect('.')
        if 'sticky_thread' in request.POST:
            q = Thread.objects.get(id=request.POST['thread'])
            q.sticky = True
            q.save()
            return HttpResponseRedirect('.')
        if 'unsticky_thread' in request.POST:
            q = Thread.objects.get(id=request.POST['thread'])
            q.sticky = False
            q.save()
            return HttpResponseRedirect('.')
        if 'lock_thread' in request.POST:
            q = Thread.objects.get(id=request.POST['thread'])
            q.locked = True
            q.save()
            return HttpResponseRedirect('.')
        if 'unlock_thread' in request.POST:
            q = Thread.objects.get(id=request.POST['thread'])
            q.locked = False
            q.save()
            return HttpResponseRedirect('.')
    data = [('forum', p),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))
    
#Thread list view.  Shows a list of all of the forum posts in a thread
#Also has options for to delete posts
def forumpost_list(request, forum_id, thread_id):
    p = get_object_or_404(Forum, id=forum_id)
    q = get_object_or_404(Thread, id=thread_id)
    template = loader.get_template('forum/forumpost_list.html')
    if not q.forum == p:
        raise Http404
    if request.method == 'POST':
        if not request.user.id: return HttpResponse("Error: Must be logged in to delete posts.")
        if 'edit_forumpost' in request.POST:
            return redirect('/forum/' + forum_id + '/' + thread_id + '/' + request.POST['forumpost'])
        if 'delete_forumpost' in request.POST:
            r = ForumPost.objects.get(id=request.POST['forumpost'])
            r.deleted = True
            r.editor = request.user
            r.dateEdited = datetime.datetime.now()
            r.save()
            return HttpResponseRedirect('.')
    data = [('forum', p), ('thread', q),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))
    
#New thread view.  Allows a logged in user to create a new thread in a forum
def new_thread(request, forum_id):
    p = get_object_or_404(Forum, id=forum_id)
    template = loader.get_template('forum/new_thread.html')
    if request.method == 'POST':
        if not request.user.id: return HttpResponse("Error: Must be logged in to create new threads.")
        form = ThreadForm(request.POST)
        if form.is_valid():
            forumpost = ForumPost()
            forumpost.body = form.cleaned_data['body']
            forumpost.author = request.user
            thread = Thread()
            thread.title = form.cleaned_data['title']
            thread.lastPostAuthor = forumpost.author
            thread.lastPostDate = forumpost.datePosted
            thread.forum = p
            thread.save()
            forumpost.thread = thread
            forumpost.save()
            return HttpResponseRedirect('.')
    else:
        form = ThreadForm()
    data = [('forum', p), ('threadform', form),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))

#New ForumPost view.  Allows a logged in user to create a new forum post in a thread
def new_forumpost(request, forum_id, thread_id):
    p = get_object_or_404(Forum, id=forum_id)
    q = get_object_or_404(Thread, id=thread_id)
    template = loader.get_template('forum/new_forumpost.html')
    if request.method == 'POST':
        if q.locked: return HttpResponse("Error: This thread is locked, you cannot edit comments in it.")
        if not request.user.id: return HttpResponse("Error: Must be logged in to edit comments.")
        form = ForumPostForm(request.POST)
        if form.is_valid():
            forumpost = ForumPost()
            forumpost.body = form.cleaned_data['body']
            forumpost.thread = q
            forumpost.author = request.user
            forumpost.save()
            return HttpResponseRedirect('.')
    else:
        form = ForumPostForm()
    data = [('forum', p), ('thread', q), ('forumpostform', form),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))

#Edit ForumPost view.  Allows the post author or an Admin to edit a forum post
def edit_forumpost(request, forum_id, thread_id, forumpost_id):
    p = get_object_or_404(Forum, id=forum_id)
    q = get_object_or_404(Thread, id=thread_id)
    r = get_object_or_404(ForumPost, id=forumpost_id)
    template = loader.get_template('forum/edit_forumpost.html')
    if request.method == 'POST':
        if q.locked: return HttpResponse("Error: This thread is locked, you cannot edit comments in it.")
        if not request.user.id: return HttpResponse("Error: Must be logged in to edit comments.")
        if 'edit_forumpost' in request.POST:
            r.body = request.POST['new_body']
            r.editor = request.user
            r.dateEdited = datetime.datetime.now()
            r.save()
            return HttpResponseRedirect('..')
    data = [('forum', p), ('thread', q), ('forumpost', r),]
    context = Context(dict(data))
    a = RequestContext(request, context)
    return HttpResponse(template.render(a))
            